import {
  queryRegionDataReq,
  queryBusinessTypeReq,
  queryCustomerReq,
  addItemReq,
  updateItemReq,
  deleteItemReq,
  toggleStateReq,
  queryItemInfoReq,
  queryRegisterReq,
} from "./services";
import { message } from "antd";

export default {
  namespace: "userRegister",
  state: {
    init: false,
    data: {},
    customer: {},
    arrRegion: [],
    businessTypeArr: [],
  },
  effects: {
    *pageInit({ params }, { call, put }) {
      const customer = yield call(queryCustomerReq, params);
      const arrRegion = yield call(queryRegionDataReq, params);
      const businessTypeArr = yield call(queryBusinessTypeReq, {
        dictType: "business_type",
      });

      yield put({
        type: "save",
        payload: {
          arrRegion,
          customer,
          businessTypeArr,
        },
      });
    },

    *submitItem({ params, callback }, { call }) {
      let res = null;
      if (params.id) {
        res = yield call(updateItemReq, params);
      } else {
        res = yield call(addItemReq, params);
      }
      if (res?.code === 200) {
        message.success(res.msg);
        callback && callback();
      } else {
        message.error(res.msg);
      }
    },

    *deleteItem({ params, callback }, { call }) {
      let res = yield call(deleteItemReq, params);
      if (res?.code === 200) {
        message.success(res.msg);
        callback && callback();
      } else {
        message.error(res.msg);
      }
    },

    *queryItemInfo({ params, callback }, { call }) {
      let res = yield call(queryItemInfoReq, params);
      if (res) {
        callback && callback(res);
      }
    },

    *toggleState({ params, callback }, { call }) {
      let res = yield call(toggleStateReq, params);
      if (res) {
        callback && callback();
      }
    },
    *queryRegister({ params, callback }, { call }) {
      let { code, msg } = yield call(queryRegisterReq, params);
      if (code === 200) {
        message.success("注册成功");
        callback && callback();
      } else {
        message.error(msg);
      }
    },
  },
  reducers: {
    save(state: any, { payload }: any) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
